/****************************************************
 * @class DccLinkGrid
 * @extends Ext.grid.GridPanel
 ***************************************************/
	DccLinkGrid = Ext.extend(Ext.grid.GridPanel,{
		cm: null,
		store: null,
		condition: null,
		documentMultiSelectionWindow: null,
		constructor: function(config){
			Ext.apply(this,config);
			
			this.cm = new Ext.grid.ColumnModel([
				{ dataIndex: 'attachment_Id', hidden:true},
				new Ext.grid.RowNumberer(),
				{ dataIndex: 'ref', width: 40, renderer: renderFileType},
				{ dataIndex: 'documentName', width: 100, sortable:false},
				{ dataIndex: 'fileName', width: 350,  sortable:false}
			]);
			
			var mastIns_id = Ext.fly('mast_id').dom.value;
			
			this.condition = {documentList: '', loadType: 'db', mast_id: mastIns_id};
			
			this.store = new Ext.data.Store({
			    baseParams: this.condition, 
			    proxy: new Ext.data.HttpProxy({
			    	method: 'POST',
					url: '/pdm/dcc/getDocumentAttachments.htm'
				}),
		        reader: new Ext.data.JsonReader({
					 totalProperty: 'totalProperty',
					 root: 'root',
					 id: 'attachment_ID'
				},[
					{name: 'attachment_ID'},
					{name: 'ref'},
					{name: 'attachmentBase'},
					{name: 'documentName'},
					{name: 'fileName'},
					{name: 'mark'}
				])
			});
						
			this.store.load();
						
			DccLinkGrid.superclass.constructor.call(this, {
				store: this.store,
				cm: this.cm,
				anchor: '98%',
				autoHeight: true,
				viewConfig: {forceFit: true},
				tbar:[{
					id: 'btn_ext_add_doc',
					text: "添加/删除",
					iconCls: 'add',
					handler:function(){
						if(typeof(this.documentMultiSelectionWindow)!="object"){
							this.documentMultiSelectionWindow = new DocumentMultiSelectionWindow({id:'documentMultiSelectionWindow', title:'Document Selection Window'});
						}
						this.documentMultiSelectionWindow.show();
					}
				},"->","<font color=brown>(提示：双击查看)</font>",{
					text: "delete",
					iconCls: 'delete',
					hidden: true,
					handler:function(){
						Ext.MessageBox.show({
							title: "温馨提示：",
							msg: "您确认要删除此文件链接？",
							buttons: Ext.MessageBox.YESNO,
							fn: function(btn){
								if(btn == 'yes')
								{
									return;
								}
								else return;
							}
						});
					}
				}]
			});
			
			this.on('rowdblclick',function(grid,rowIndex,e){
				var record = grid.getSelectionModel().getSelected();
				
				var bAuth = false;
				bAuth = bAuthedReadDccDoc(record.get("documentName"), record.get("mark"));
				if(bAuth){
					var url = "";
					if(record.get("mark")=="sign"){
						url = "http://10.20.2.82:8080/Cyrus1/servlet/DccProcessFileDLDownloadServlet?accessCode="+record.get("attachmentBase")+"&orderTask=35&hasToken=true";
					}else{
						url = "http://10.20.2.82:8080/Cyrus1/servlet/DccFileDLDownloadServlet?accessCode="+record.get("attachmentBase")+"&strTypeFlag=DR";
					}
					
					window.open(url, '文档在线阅读', 'height=640, width=800, toolbar=no, menubar=no, scrollbars=no, resizable=yes, location=no, status=yes');
					
				}else{
					Ext.Msg.alert("提示:","您没有查看此文件的权限，请进DCC系统进行申请权限！");
				}
				
			});
		},
		load: function(c){
			this.condition.loadType = "page";
			this.condition.documentList = c;
			this.store.load();
		}
	});
	
	
/**
 * DCC LINK模块
 * @description 可以登录到DCC相关表单进行查阅
 * @class DccLinkGrid2
 * @extends Ext.grid.GridPanel
 */
DccLinkGrid2 = Ext.extend(Ext.grid.GridPanel,{
	cm: null,
	store: null,
	condition: null,
	mast_id: 0,
	auth: true,
	documentMultiSelectionWindow: null,
	constructor: function(config){
		Ext.apply(this,config);
					
		function renderProcessLog(value,metadata,record,rowIndex,colIndex,store){
			//metadata.attr = 'style="cursor:hand;"';
			value = "<a href=\"javascript:openLog(null,'"+record.get("trackingNumber")+"','"+record.get("documentNumber")+"');\">"+value+"</a>";
			return value;
		}
		
		this.cm = new Ext.grid.ColumnModel([
			new Ext.grid.RowNumberer(),
			{ dataIndex: 'documentNumber', header: "文件编号", width: 100, sortable: false },
			{ dataIndex: 'version', header: "版本", width: 60, sortable: false },
			{ dataIndex: 'documentName', header: "文件名称", width: 350, sortable: false },
			{ dataIndex: 'more', width: 30, renderer: renderProcessLog }
		]);
				
		this.condition = {documentList: '', loadType: 'db', mast_id: this.mast_id};
		
		this.store = new Ext.data.Store({
			autoLoad: true,
		    baseParams: this.condition, 
		    proxy: new Ext.data.HttpProxy({
		    	method: 'POST',
				url: '/pdm/dcc/getDocuments.htm'
			}),
	        reader: new Ext.data.JsonReader({
				 totalProperty: 'totalProperty',
				 root: 'root',
				 id: 'documentNumber'
			},[
				{name: 'documentNumber'},
				{name: 'documentName'},
				{name: 'version'},
				{name: 'trackingNumber'},
				{name: 'documentVersion_ID'},
				{name: 'mark'},
				{name: 'more'}
			])
		});
								
		DccLinkGrid2.superclass.constructor.call(this, {
			store: this.store,
			cm: this.cm,
			autoHeight: true,
			viewConfig: {forceFit: true},
			tbar:[{
				id: 'btn_ext_add_doc_2',
				text: "添加",
				iconCls: 'add',
				scope: this,
				disabled: !this.auth,
				handler: function(){
					if(Ext.isEmpty(this.documentMultiSelectionWindow)){
						this.documentMultiSelectionWindow = new DocumentMultiSelectionWindow({id:'documentMultiSelectionWindow', title:'Document Selection Window'});
					}
					this.documentMultiSelectionWindow.show();
				}
			},'-',{
				id: 'btn_ext_del_doc_2',
				text: "删除",
				iconCls: 'delete',
				scope: this,
				disabled: !this.auth,
				handler: function(){
					var record = this.getSelectionModel().getSelected();
					if(Ext.isEmpty(record))
					{
						Ext.Msg.alert("提示","请选择需要删除的文件!");
					}else{
						this.store.remove(record);
						var collectDocument = Ext.fly("_collectDocument").dom.value;
		
						var collectDocuments = collectDocument.split(",");
						var j = collectDocuments.length;
						for(var i=0; i < j; i++)
						{
							if(collectDocuments[i] == record.get("documentNumber"))
							{
								for(var t=i;t<j;t++)
								{
									collectDocuments[t]=collectDocuments[t+1];
								}
								j=j-1;
							}
						}
						if(j==0) collectDocument = "";
						else
						{
							for(var m=0;m<j;m++)
							{
								if(m==0) collectDocument=collectDocuments[m];
								else collectDocument = collectDocument+","+collectDocuments[m];
							}
						}
						Ext.fly("_collectDocument").dom.value = collectDocument;
						
					}
					
				}
			},"->","<font color=brown>(提示：双击查看)</font>"]
		});
		
		this.on('rowdblclick',function(grid, rowIndex, e){
			var record = grid.getSelectionModel().getSelected();
							
			if(record.get("mark")=="sign")
			{
				if(record.get("trackingNumber")==" ") Ext.Msg.alert("提示","联系管理员！");
				else{
					var screen_ID = "DCC.DER";
					if(record.get("version") == "00") screen_ID = "DCC.DCR";
					openDcc(record.get("trackingNumber"), screen_ID, "sign" );
				}
			}else{
				var bAuth = false;
				bAuth = bAuthedReadDccDoc(record.get("documentNumber"), record.get("mark"));
				if(!bAuth) bAuth = bDccDocAuth(record.get("documentVersion_ID"));
				if(bAuth)
				{
					if(record.get("documentVersion_ID") == '-1') Ext.Msg.alert("提示","系统遇到问题，请联系管理员");
					else openDcc(record.get("documentVersion_ID"), "DCC.DFR", "issue" );
				}
				else Ext.Msg.alert("提示:","您没有查看此文件的权限，请进DCC系统进行申请权限！");
			}
		});
	},
	load: function(c){
		this.condition.loadType = "page";
		this.condition.documentList = c;
		this.store.load();
	}
});	

function openDcc(v, screen_ID, action){
	jsonrpc = new JSONRpcClient("/pdm/JSON-RPC");
	try{
		var info = "";
		if(action== "sign") info = jsonrpc.jabsorbService.getDispatchUrl(v, Ext.fly("login_user").dom.value, screen_ID);
		else if(action== "issue") info = jsonrpc.jabsorbService.getDispatchDccUrl(v, Ext.fly("login_user").dom.value, screen_ID);
	}catch(e){
		alert(e);
	}
	if(action== "sign") window.open("http://10.20.2.244:8080/Arcadia/servlet/WebDispatcher?mailCode="+info);
	else if(action== "issue") window.open("http://10.20.2.244:8080/Arcadia/servlet/WebDispatcherDcc?mailCode="+info);
}

function openLog(v,c,m){
	if(!v) {v = new WorkFlowWindow();}
	v.setTitle("["+m+"] 流程日志");
	v.show();
	v.load(c);
}
	